/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.ce.businesslogic;

import com.bionic.ce.dao.IBankaccDAO;
import com.bionic.ce.beans.Bankacc;
import com.bionic.ce.connections.DAOFactory;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author Jarik
 */
public class BankAccService {

    public static IBankaccDAO bankaccDAO;

    static {
        DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.MYSQL);
        bankaccDAO = factory.getBankaccDAO();
    }

    public static void createNewBankacc(Integer accNum, String currency, String bankName,
            String swift, String correspondentBank, Integer userId) {
        Bankacc bankacc = new Bankacc();
        bankacc.setAccNum(accNum);
        bankacc.setCurrency(currency);
        bankacc.setBankName(bankName);
        bankacc.setSwift(swift);
        bankacc.setCorrespondentBank(correspondentBank);
        bankacc.setUserId(userId);
        if (accNum == null | currency.isEmpty() | bankName.isEmpty() | swift.isEmpty() | correspondentBank.isEmpty()) {
            throw new IllegalStateException("Not all required fields are filled");
        } else {
            try {
                bankaccDAO.add(bankacc);
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void updateCurrentBankacc(Integer Id, Integer accNum, String currency, String bankName,
            String swift, String correspondentBank, Integer userId) {
        Bankacc bankacc = new Bankacc();
        bankacc.setId(Id);
        bankacc.setAccNum(accNum);
        bankacc.setCurrency(currency);
        bankacc.setBankName(bankName);
        bankacc.setSwift(swift);
        bankacc.setCorrespondentBank(correspondentBank);
        bankacc.setUserId(userId);
        if (accNum == null | currency.isEmpty() | bankName.isEmpty() | swift.isEmpty() | correspondentBank.isEmpty()) {
            throw new IllegalStateException("Updates was not effected because not all required fields were filled");
        } else {
            try {
                bankaccDAO.update(bankacc);
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void deleteCurrentBankacc(Integer bankaccId) {
        try {
            bankaccDAO.deleteById(bankaccId);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public static List<Bankacc> returnBankAccountsInfo(Integer userId) {
        List<Bankacc> list = null;
        Bankacc bankacc = new Bankacc();
        bankacc.setUserId(userId);
        try {
            list = bankaccDAO.findByDynamicSelect(bankacc);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return list;
    }

    public static List<Bankacc> returnBankAccountsInfo(Integer userId, String currency) {
        List<Bankacc> list = null;
        Bankacc bankacc = new Bankacc();
        bankacc.setUserId(userId);
        bankacc.setCurrency(currency);
        try {
            list = bankaccDAO.findByDynamicSelect(bankacc);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return list;
    }

    public static Bankacc returnCurrentBankAccInfo(Integer bankAccId) {
        Bankacc bankAcc = null;
        Bankacc bankacc = new Bankacc();
        if (bankAccId==null) 
            throw new IllegalStateException("Invalid Bank account ID");
        else bankacc.setId(bankAccId);

        try {
            bankAcc = (Bankacc) ((bankaccDAO.findByDynamicSelect(bankacc)).toArray()[0]);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return bankAcc;
    }
}
